﻿$(function () {
    $("#submit").click(function () {
        Submit();
    });

    $("#body").keypress(function (event) {
        if (event.which == '13') {
            Submit();
        }
    });
});

function Submit() {

    $("#submit").hide();

    var input = {
        ID: $("#id").val(),
        Password: $("#Password").val(),
        Password1: $("#Password1").val(),
        Password2: $("#Password2").val()
    };

    Ajax({
        url: "/Console/SavePassword",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == false) {
                ShowValidationErrors(data);
            } else if (data.IsSaved == true) {
                location.href = "/Console/Password?msg=1";
            }
        },
        complete: function () {
            $("#submit").show();
        }
    });
}

function ValidationErrors(data) {

    var msg = "";
    $("#Passwordval").remove();
    $("#Password1val").remove();
    $("#Password2val").remove();

    if (data.PasswordValidationCode > 0) {
        if (data.PasswordValidationCode == 1) {
            msg = "Please enter";
        } else if (data.PasswordValidationCode == 2) {
            msg = "cannot be more than 50 characters";
        } else if (data.PasswordValidationCode == 3) {
            msg = "wrong password";
        }
        $("#Password").focus();
        $("#Password").after('<span id="Passwordval" class="input-notification error png_bg">' + msg + '</span>');
    }

    if (data.Password1ValidationCode > 0) {
        if (data.Password1ValidationCode == 1) {
            msg = "Please enter";
        } else if (data.Password1ValidationCode == 2) {
            msg = "cannot be more than 50 characters";
        }
        $("#Password1").focus();
        $("#Password1").after('<span id="Password1val" class="input-notification error png_bg">' + msg + '</span>');
    }

    if (data.Password2ValidationCode > 0) {
        if (data.Password2ValidationCode == 1) {
            msg = "Please enter";
        } else if (data.Password2ValidationCode == 2) {
            msg = "cannot be more than 50 characters";
        } else if (data.Password2ValidationCode == 3) {
            msg = "does not match";
        }
        $("#Password2").focus();
        $("#Password2").after('<span id="Password2val" class="input-notification error png_bg">' + msg + '</span>');
    }
}